﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyOA.BLL;
using MyOA.Model;

namespace MyOA.UI.File.FileManage
{
    public partial class FileLeft : System.Web.UI.Page
    {
        FileLeftManage manage = new FileLeftManage();

        /// <summary>
        /// 生成子节点（递归）
        /// </summary>
        /// <param name="fileId">通过文件ID来查找文件信息</param>
        /// <param name="parentNode">父节点</param>
        private void createChildTreeNode(int fileId, TreeNode parentNode)
        {
            IList<FileInfo> fileInfos = manage.getFileInfoByParentId(fileId);





            foreach (FileInfo fileInfo in fileInfos)
            {
                TreeNode tnChild = new TreeNode(fileInfo.FilesName, fileInfo.FileId.ToString(), fileInfo.FileType.FileTypeImage, "FileMain.aspx?fileId=" + fileInfo.FileId, "ifrFileRight");
                tnChild.ToolTip = fileInfo.FilesName;
                //把节点添加到父节点里去（父节点是传进来的参数）
                parentNode.ChildNodes.Add(tnChild);
                //递归调用（因为fileInfos可能没有数据 所以不用递归出口 不会成死循环）
                createChildTreeNode(fileInfo.FileId, tnChild);


            }
        }

        /// <summary>
        /// 绑定树形控件
        /// </summary>
        public void dataBind()
        {

            IList<FileInfo> parentFile = manage.getFileInfoByParentId(0);



            //循环跟节点
            foreach (FileInfo parent in parentFile)
            {
                TreeNode tnParent = null;
                if (parent.ParentId == 0)
                {
                    tnParent = new TreeNode(parent.FilesName, parent.FileId.ToString(), parent.FileType.FileTypeImage, "FileMain.aspx?fileId=" + parent.FileId, "ifrFileRight");
                    tnParent.ToolTip = parent.FilesName;
                    tvFileInfo.Nodes.Add(tnParent);

                    //跟节点下添加无限级子节点
                    createChildTreeNode(parent.FileId, tnParent);
                }
            }
            tvFileInfo.CollapseAll();

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
                return;
            }
            if (!IsPostBack)
            {

                dataBind();
            }
        }
    }
}
